using System;
using System.Reflection;
using System.Collections.Generic;

using Dashboard;
using Dashboard.Util;

namespace Dashboard.Mint.Actions {

	public static class ActionFactory {
		
		private static List<Action> actions = new List<Action> ();

		static ActionFactory ()
		{
			List<Type> types = ReflectionFu.ScanAssemblyForClass (Assembly.GetExecutingAssembly(), typeof (Action));
			
			foreach (Type action_type in types) { 
				object[] attributes = Attribute.GetCustomAttributes (action_type, typeof (ActionAttribute));
				
				foreach (object attribute in attributes) {
					ActionAttribute action_attribute = attribute as ActionAttribute;
					
					if (action_attribute == null)
						continue;
					
					Action action = System.Activator.CreateInstance (action_type) as Action;

					actions.Add (action);
				}
			}
		}

		public static IEnumerable<Action> BuildActions (Match match)
		{
			// FIXME: This totally sucks. We should drop the
			// action attribute and move it into a property instead.
			// Much better! :-)

			foreach (Action action in actions) {
				object[] attributes = Attribute.GetCustomAttributes (action.GetType (), typeof (ActionAttribute));
				
				foreach (object attribute in attributes) {
					ActionAttribute action_attribute = attribute as ActionAttribute;
					
					if (action_attribute == null)
						continue;

					if (action_attribute.Type == match.Type)
						yield return action;
				}
			}
		}
	}
}